

<title>维护操作 - 系统管理</title>

<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">首页</a>
    <a><cite>系统管理</cite></a>
    <a><cite>维护操作</cite></a>
  </div>
</div>

<style type="text/css">
.layui-elem-quote{padding: 6px;}
.thin-gray{font-weight: 400;color: #999;} 
.small-title{font-weight:700;font-size:14px}
hr {margin-bottom: 30px}
.layui-form-switch {margin-top: 0px;}
</style>
<div class="layui-fluid">
  <div class="layui-row layui-col-space15">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body">
         <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
          <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">

              <div class="layui-row ">
               <div class="layui-col-lg2 layui-col-md4"> <span class="small-title">节点设置</span></div>
              </div>

              <div class="layui-row layui-col-space10 ">
                <div class="layui-col-lg2 layui-col-md4">节点配置的主控IP：</div>
                <div class="layui-col-lg2 layui-col-md4 layui-form">
                  <input type="text" name="master_ip" required lay-verify="required" placeholder="填写主控IP" autocomplete="off" class="layui-input">    
                </div>
                <div class="layui-form-mid layui-word-aux">留空则不设置</div>
              </div> 

              <div class="layui-row layui-col-space10 ">
                <div class="layui-col-lg2 layui-col-md4">节点配置的Elasticsearch IP：</div>
                <div class="layui-col-lg2 layui-col-md4 layui-form">
                  <input type="text" name="es_ip" required lay-verify="required" placeholder="通常填写主控IP" autocomplete="off" class="layui-input">    
                </div>
                <div class="layui-form-mid layui-word-aux">留空则不设置</div>
              </div> 

              <div class="layui-row layui-col-space10 ">
                <div class="layui-col-lg2 layui-col-md4">节点配置的ES密码：</div>
                <div class="layui-col-lg2 layui-col-md4 layui-form">
                  <input type="text" name="es_pwd" required lay-verify="required" placeholder="用于主控连接节点及节点连接ES的密码" autocomplete="off" class="layui-input">    
                </div>
                <div class="layui-form-mid layui-word-aux">留空则不设置</div>
              </div> 

              <div class="layui-row layui-col-space10 ">
                <div class="layui-col-lg2 layui-col-md4">节点配置的主控域名：</div>
                <div class="layui-col-lg2 layui-col-md4 layui-form">
                  <input type="text" name="master_host" required lay-verify="required" placeholder="用于回源主控88端口设置的Host请求头" autocomplete="off" class="layui-input">    
                </div>
                <div class="layui-form-mid layui-word-aux">留空则不设置</div>
              </div>

              <div class="layui-row layui-col-space10 ">
                <div class="layui-col-lg2 layui-col-md4"></div>
                <div class="layui-col-lg2 layui-col-md4 layui-form">
                  <button type="button" id="save" class="layui-btn layui-btn-sm">创建设置任务</button>  
                  <button style="margin-left:20px;" type="button" id="view-task" class="layui-btn layui-btn-sm layui-btn-normal">查看任务</button>  
                </div>
              </div>

            </div>
        </div>
      </div>                 

    </div>
  </div>
</div>
</div>
</div>

<script type="text/javascript">
  layui.use(['admin', 'table','form'], function(){
    var $ = layui.$
    ,admin = layui.admin
    ,view = layui.view
    ,table = layui.table
    ,form = layui.form;

    form.render()

    $("input[name='master_ip']").on("change",function(){
      var value = $("input[name='master_ip']").val()
      $("input[name='es_ip']").val(value)
    })

    // 查看任务
    $("#view-task").click(function() {
      if (typeof(window.task_id) == undefined || window.task_id == null || window.task_id == "" ) {
        layer.alert("未创建任务")
        return
      }
      
      location.hash = "/system/task/id="+window.task_id+"/"
    })

    // 填充
    admin.req({
      url: '/configs/global-0-system-node-config'
      ,type: "get"
      ,contentType:"application/json"
      ,dataType: "json"        
      ,done: function(res){
        window.task_id = res.data.task_id
      }
    });

    // 保存
    $("#save").click(function (argument) {
      layer.prompt({
        formType: 1,
        title: '请输入当前用户的密码'
      }, function(value, index, elem){
        put_config(value)
        layer.close(index);
      });      
    })

    function put_config(password) {
      var master_ip = $("input[name='master_ip']").val()
      var es_ip = $("input[name='es_ip']").val()

      var master_host = $("input[name='master_host']").val()
      var es_pwd = $("input[name='es_pwd']").val()
      var rnd = Math.ceil(Math.random()*100);

      var data = {"es_ip":es_ip, "password":password, "master_ip":master_ip, "master_host":master_host, "es_pwd":es_pwd,"rnd":rnd}
      var req_data = {"value": JSON.stringify(data) }

      admin.req({
        url: '/configs/global-0-system-node-config' //实际使用请改成服务端真实接口
        ,type: "put"
        ,data: JSON.stringify(req_data)
        ,contentType:"application/json"
        ,dataType: "json"        
        ,done: function(res){
          //登入成功的提示与跳转
          layer.msg('创建成功，可点击查看任务查看进度', {
              icon: 1,
              time: 1000 //2秒关闭（如果不配置，默认是3秒）
            }, function(){
              layui.index.render();
            });   

        }
      });
    }


  });  

</script>